//处理参数对象
const setParams = (params) => {
  let queryArr = [];
  for (let key in params) {
    let str = key + "=" + params[key];
    queryArr.push(str);
  }
  return queryArr.join("&");
};
const ldgAjax = (option) => {
  return new Promise((res, rej) => {
    const xhr = new XMLHttpRequest();
    const query = setParams(option.data);
    xhr.responseType = "json";
    if (option.method.toUpperCase() === "GET") {
      xhr.open("GET", query ? option.url + "?" + query : option.url);
      xhr.send();
    } else if (option.method.toUpperCase() === "POST") {
      xhr.open("POST", option.url);
      xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xhr.send(query);
    }
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          res(xhr.response);
        } else {
          rej(xhr.status);
        }
      }
    };
  });
};
